package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    @GetMapping
    public Result queryClazzs(
            String name,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        PageResult<Clazz> pageResult = clazzService.findAll(name, begin, end, page, pageSize);

        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    public Result queryClazzById(@PathVariable Integer id) {
        Clazz clazz = clazzService.queryClazzById(id);
        return Result.success(clazz);
    }

    @DeleteMapping("/{id}")
    public Result deleteClazzById(@PathVariable Integer id) {
        clazzService.deleteClazzById(id);
        return Result.success();
    }

    @PostMapping
    public Result insertClazz(@RequestBody Clazz clazz) {
        clazzService.insertClazz(clazz);
        return Result.success();
    }

    @PutMapping
    public Result updateClazz(@RequestBody Clazz clazz) {
        clazzService.updateClazz(clazz);
        return Result.success();
    }

    @GetMapping("/list")
    public Result queryAllClazz() {
        List<Clazz> clazz = clazzService.queryAllClazz();
        return Result.success(clazz);
    }
}
